#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2019 crane <crane@crane-pc>
#
# Distributed under terms of the MIT license.

"""

"""

board_bound = 8


class Horse:
    def __init__(self, init_pos):
        # init_row, init_col = init_pos
        # self.init_pos
        self.visited = set()
        self.order = []

        self.visited.add(init_pos)
        self.order.append(init_pos)

        self.horse(init_pos)

        print(self.order)

    def horse(self, pos):
        row, col = pos

        for new_pos in self.eight_pos(pos):
            if not self.valid_pos(new_pos):
                continue

            self.order.append(new_pos)
            self.visited.add(new_pos)

            if len(self.visited) == 64:
                return True

            ret = self.horse(new_pos)
            if ret:
                return ret
            else:
                self.order.pop()
                self.visited.remove(new_pos)

        # for new_row in [row-1, row+1]:
        #     if not self.valid_num(new_row):
        #         continue

        #     for new_col in [col-2, col+2]:
        #         if not self.valid_num(new_col):
        #             continue

        #         if not self.valid_pos( (new_row, new_col) ):
        #             continue

    def eight_pos(self, pos):
        row, col = pos

        for new_row in [row-1, row+1]:
            for new_col in [col-2, col+2]:
                yield new_row, new_col

        for new_col in [col-1, col+1]:
            for new_row in [row-2, row+2]:
                yield new_row, new_col

    def valid_num(self, n):
        # n is row/col
        if n < 0 or n >= board_bound:
            return False
        return True

    def valid_pos(self, pos):
        x, y = pos

        if not self.valid_num(x):
            return False

        if not self.valid_num(y):
            return False

        return pos not in self.visited


def main():
    print("start main")

if __name__ == "__main__":
    main()
    # Horse( (0, 0) )
    Horse( (1, 1) )
